#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MOD = 1e9 + 7;

void solve()
{
    int n;
    cin >> n;
    vector<int> a(n), b(n), c(n);
    vector<int> mp(n + 1);
    for(int i = 0; i < n; i++) cin >> a[i];
    for(int i = 0; i < n; i++) cin >> b[i];
    for(int i = 0; i < n; i++){
        cin >> c[i];
        if(c[i]) mp[c[i]] = 1;
    } 
    ll ans = 1;
    for(int i = 0; i < n; i++) {
        if(c[i]) continue;
        int f = 0;
        if(a[i] != b[i] && mp[a[i]] == 0 && mp[b[i]] == 0){
            ans = ans * 2 % MOD;
        }     
        mp[a[i]] = 1;
        mp[b[i]] = 1;
    }
    cout << ans << endl;
}

int main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--)
        solve();
    return 0;
}